package com.pl.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.pl.dao.StudentDao;
import com.pl.entity.Student;
import com.pl.lister.StudentReadLister;
import com.pl.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author 小胤
 * @Date 2024/4/25 20:08
 * @注释
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Override
    public boolean finAllStudent() {
        List<Student>students = studentDao.finAllStudent();
        if (students!=null){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateStudentById(Student student) {
        //num 就是受影响的行数
        int num = studentDao.updateStudentById(student);
        if (num > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean removeStudentById(int id) {
        int i = studentDao.removeStudentById(id);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean addStudent(Student student) {
        int i = studentDao.addStudent(student);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public int addStudents(List<Student> list) {

        return studentDao.addStudents(list);
    }

    @Override
    public String importFile(MultipartFile file) {
        //导入,获取页面上的文件,将文件数据转化为数据库存储数据,然后反馈到页面,刷新展示
        try {
            EasyExcel.read(file.getInputStream(), Student.class, new StudentReadLister(this))
                    .sheet("学生信息").doReadSync();
            return "import_success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";
    }

    @Override
    public void exportFile(HttpServletResponse response) {
        List<Student> students = studentDao.finAllStudent();
        try {
            EasyExcel.write(response.getOutputStream(), Student.class)
                    .sheet("学生信息")
                    .doWrite(students);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 分页查询学生列表信息以及模糊查询
     * @param map
     * @return
     */
    @Override
    public List<Student> queryByPage(Map<String, Object> map) {
        return studentDao.queryByPage(map);
    }

    @Override
    public boolean deleteBatch(List<Integer> list) {
        int num=studentDao.deleteBatch(list);
        if (num>0){
            return  true;
        }
        return false;
    }
}
